package com.lex.util;

import com.lex.data.admin.model.LexCmsUsers;
import com.lex.entity.user.UserResourceVo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class AdminUserDetails implements UserDetails {
    private String username;
    private String password;
    private List<UserResourceVo> authorityList;

    private Byte status = 1;

    public AdminUserDetails() {
    }
    public AdminUserDetails(LexCmsUsers user, List<UserResourceVo> authorityList) {
        this.username = user.getAccount();
        this.password = user.getPassword();
        this.status = user.getStatus();
        this.authorityList = authorityList;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorityList == null
                ? null
                : this.authorityList.stream()
                .map(map -> new SimpleGrantedAuthority(map.getUrl()+"@"+map.getMethod()))
                .collect(Collectors.toList());
    }

    @Override
    public String toString() {
        return "AdminUserDetails{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", authorityList=" + authorityList +
                '}';
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}
